package concurrent;


import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierDemo {

    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(2, new Runnable() {
            @Override
            public void run() {
                System.out.println("等待所有子线程准备完毕，可以继续执行了");
            }
        });

        new Thread(() -> {
            System.out.println("线程2开始开始准备");
            try {
                cyclicBarrier.await();
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("线程2执行完毕");
        }).start();

        new Thread(() -> {
            System.out.println("线程1开始开始准备");
            try {
                cyclicBarrier.await();
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("线程1执行完毕");
        }).start();
    }
}
